package one.stand.event;

import com.jmp.base.MessageApi;
import com.jmp.base.dto.MessagePushRequest;
import com.jmp.base.dto.MessagePushType;
import com.jmp.base.dto.MessageWxSubscribeDto;
import com.jmp.feign.MessageFeign;
import lombok.extern.slf4j.Slf4j;
import one.stand.enums.LoginEnum;
import one.stand.mapper.LiveRoomMapperExt;
import one.stand.mapper.LiveUserMapperExt;
import one.stand.model.BusinessCard;
import one.stand.model.Company;
import one.stand.model.LiveRoom;
import one.stand.model.LiveUser;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.CompanyBaseService;
import one.stand.service.RecordPublicService;
import one.stand.service.user.TokenService;
import one.stand.util.DateUtil;
import one.stand.util.OneListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.List;

@Component
@Async("asyncServiceExecutor")
@Slf4j
public class LiveRoomListener {

    @Autowired
    private MessageFeign messageFeign;

    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private RecordPublicService recordPublicService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private LiveRoomMapperExt liveRoomMapperExt;
    @Autowired
    private LiveUserMapperExt liveUserMapperExt;

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void handle(LiveRoomOpenEvent event) {
        log.info("event:{}", event.getSource());

        LiveRoom liveRoom = liveRoomMapperExt.selectByPrimaryKey(event.getRoomId());
        LiveUser liveUser = liveUserMapperExt.selectByPrimaryKey(liveRoom.getLiveUserId());
        BusinessCard card = businessCardBaseService.getCustomer(liveUser.getCardId());
        Company company = companyBaseService.getById(card.getCompanyId());

        MessagePushRequest pushRequest = MessagePushRequest.builder()
                .type(MessagePushType.LIVE_ROOM_OPEN)
                .toType(MessageApi.TYPE_COLLECTION)
                .toChildType(RecordPublicService.ITEMTYPE_LIVE_ROOM_PRE)
                .toId(event.getRoomId())
                .fillData(new String[]{company.getCompanyName()})
                .pageParams(new Object[]{event.getRoomId()})
                .build();
        messageFeign.push(pushRequest);

        List<Integer> userIds = recordPublicService.getCollection(RecordPublicService.ITEMTYPE_LIVE_ROOM_PRE, event.getRoomId());
        if (OneListUtil.isNotEmpty(userIds)) {
            MessageWxSubscribeDto messageDto;
            for (Integer toId : userIds) {
                if (tokenService.hasLoginStatus(card.getUserId(), LoginEnum.CRM)) {
                    messageDto = MessageWxSubscribeDto.builder()
                            .type(MessageApi.MSG_LIVE_OPEN)
                            .toId(toId)
                            .pageArr(new String[]{String.valueOf(liveUser.getCardId()), String.valueOf(company.getCompanyId()), String.valueOf(event.getRoomId())})
                            .dataArr(new String[]{DateUtil.getDay()})
                            .build();
                    messageFeign.wxSubscribe(messageDto);
                }
            }
        }
    }
}
